package com.relytosoft.mizar.media.analyze.services.service.structure.impl;

import com.relytosoft.mizar.media.alarm.api.entity.Alarm;
import com.relytosoft.mizar.media.alarm.api.feign.AlarmFeignInService;
import com.relytosoft.mizar.media.analyze.services.service.structure.AyStructureAlarmService;
import com.relytosoft.mizar.media.analyze.services.service.system.AyUploadImageService;
import com.relytosoft.mizar.media.common.entity.algo.AYVehicle;
import com.relytosoft.mizar.media.common.entity.algo.AYVideoSliceCommon;
import com.relytosoft.mizar.media.common.entity.algo.AyControlStructure;
import com.relytosoft.mizar.media.common.entity.pojo.dto.AyDispositionPlanDto;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlStructureControlReq;
import com.relytosoft.mizar.media.common.enums.UpLoadFileEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author wss
 * @version 1.0
 * @date 2022/6/22 14:01
 */
@Service
public class AyStructureAlarmServiceImpl implements AyStructureAlarmService {

    @Autowired
    AlarmFeignInService alarmFeignInService;
    @Autowired
    AyUploadImageService ayUploadImageService;

    @Override
    public void structureCoverVehicleAlarm(AYVehicle ayVehicle, Map<String, String> bigPic, String deviceId) {
        rawDataUrlReplace(bigPic, ayVehicle, UpLoadFileEnum.ALGO_STRUCTURE);
        Alarm alarm = Alarm.buildCoverVehicleAlarm(ayVehicle, bigPic, deviceId);
        alarmFeignInService.handTaskAlarm(alarm);
    }

    @Override
    public void structureAlarm(AyDispositionPlanDto dispositionPlanDto, Map<String, String> bigPic, AYVideoSliceCommon ayVideoSliceCommon) {
        rawDataUrlReplace(bigPic, ayVideoSliceCommon, UpLoadFileEnum.ALGO_STRUCTURE);
        Alarm alarm = Alarm.buildStructureAlarm(dispositionPlanDto, bigPic, ayVideoSliceCommon);
        alarmFeignInService.handTaskAlarm(alarm);
    }

    @Override
    public void structureControlAlarm(AyDispositionPlanDto ayDispositionPlanDto, BtdlStructureControlReq.Alarm structureAlarm) {
        controlStructureDataUrlReplace(structureAlarm);
        Alarm alarm = Alarm.buildStructureControlAlarm(structureAlarm, ayDispositionPlanDto);
        alarmFeignInService.handTaskAlarm(alarm);
    }

    /**
     * 结构化任务原始数据图片替换
     *
     * @param bigPic
     */
    public void rawDataUrlReplace(Map<String, String> bigPic, AYVideoSliceCommon ayVideoSliceCommon, UpLoadFileEnum upLoadFileEnum) {
        for (String key : bigPic.keySet()) {
            String base64FrameUpload = ayUploadImageService.base64FrameUpload(bigPic.get(key),
                    ayVideoSliceCommon.getLeftTopX(),
                    ayVideoSliceCommon.getLeftTopY(),
                    ayVideoSliceCommon.getRightBtmX(),
                    ayVideoSliceCommon.getRightBtmY(),
                    upLoadFileEnum.getCode());
            bigPic.put(key, base64FrameUpload);
        }
    }

    /**
     * 布控结构化任务数据图片替换
     *
     * @param alarm
     */
    public void controlStructureDataUrlReplace(BtdlStructureControlReq.Alarm alarm) {
        AyControlStructure alarmData = alarm.getAlarmData();
        String rawUrlFrameUpload = ayUploadImageService.rawUrlFrameUpload(alarmData.getImage(),
                Integer.parseInt(alarmData.getLeftTopX()),
                Integer.parseInt(alarmData.getLeftTopY()),
                Integer.parseInt(alarmData.getRightBtmX()),
                Integer.parseInt(alarmData.getRightBtmY()),
                UpLoadFileEnum.ALGO_STRUCTURE_CONTROL.getCode()
        );
        alarmData.setImage(rawUrlFrameUpload);
    }
}
